package com.zhengsr.wanandroid;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.zhengsr.cusbottomlib.IBottomClickListener;
import com.zhengsr.cusbottomlib.view.CusBottomLayout;
import com.zhengsr.wanandroid.fragment.HomeFragment;
import com.zhengsr.wanandroid.fragment.NaviFragment;
import com.zhengsr.wanandroid.fragment.SystemKnowFragment;
import com.zhengsr.wanandroid.fragment.UserFragment;

import me.yokeyword.fragmentation.SupportActivity;

public class MainActivity extends SupportActivity implements IBottomClickListener {
    private CusBottomLayout mCusBottomLayout;
    private HomeFragment mHomeFragment;
    private UserFragment mUserFragment;
    private SystemKnowFragment mSystemKnowFragment;
    private NaviFragment mNaviFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();
        mHomeFragment = HomeFragment.newInstance();
        if (findFragment(HomeFragment.class)== null) {
            loadRootFragment(R.id.bottom_content, mHomeFragment);
        }
    }

    private void initView() {
        mCusBottomLayout = findViewById(R.id.bottom_ly);
        mCusBottomLayout.setBottomClickListener(this);
    }

    @Override
    public void onClick(View view, int i) {
        android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        resetFragment(ft);
        switch (i){
            case 0:
                if (mHomeFragment  == null){
                    mHomeFragment = HomeFragment.newInstance();
                    ft.add(R.id.bottom_content,mHomeFragment);
                }else{
                    ft.show(mHomeFragment);
                }
                break;
            case 1 :
                if (mSystemKnowFragment == null){
                    mSystemKnowFragment = SystemKnowFragment.newInstance();
                    ft.add(R.id.bottom_content,mSystemKnowFragment);
                }else{
                    ft.show(mSystemKnowFragment);
                }
                break;
            case 2 :
                if (mNaviFragment == null){
                    mNaviFragment = NaviFragment.newInstance();
                    ft.add(R.id.bottom_content,mNaviFragment);
                }else{
                    ft.show(mNaviFragment);
                }
                break;
            case 4:
                //start(UserFragment.newInstance(), SINGLETASK);
                if (mUserFragment  == null){
                    mUserFragment = UserFragment.newInstance();
                    ft.add(R.id.bottom_content,mUserFragment);
                }else{
                    ft.show(mUserFragment);
                }
                break;
            default:
                break;
        }
        ft.commit();
    }

    private void resetFragment(FragmentTransaction ft) {
        if (mHomeFragment != null){
            ft.hide(mHomeFragment);
        }

        if (mSystemKnowFragment != null){
            ft.hide(mSystemKnowFragment);
        }
        if (mNaviFragment != null){
            ft.hide(mNaviFragment);
        }

        if (mUserFragment != null){
            ft.hide(mUserFragment);
        }
    }
}
